package com.hospital.controller;

import com.github.pagehelper.PageInfo;
import com.hospital.mapper.HospusersMapper;
import com.hospital.pojo.Hospusers;
import com.hospital.pojo.HospusersExample;
import com.hospital.pojo.Role;
import com.hospital.pojo.UserHardCoded;
import com.hospital.service.LoginOutService;
import com.hospital.vo.ResultVO;
import com.hospital.vo.UserWithRole;
import org.apache.ibatis.annotations.Param;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class LoginOut {
    @Resource
    private LoginOutService loginOutService;

//    @RequestMapping(value = "/login.do", method = RequestMethod.POST)
//    public void loginOut(String username, String password, HttpServletRequest hreq, HttpServletResponse hresp) throws IOException, InterruptedException {
//        System.out.println(username);
//        System.out.println(password);
//        UserHardCoded login = loginOutService.login(username, password);
//        if (login == null){
//            hresp.setContentType("text/html;charset=utf-8");
//            hresp.getWriter().write("<script>alert('登录失败，请重新登录！');location.href='/login.html';</script>");
//        } else {
//            hreq.getSession().invalidate();
//            hreq.getSession().setAttribute("user", login);
//            hresp.setContentType("text/html;charset=utf-8");
//            hresp.getWriter().write("<script>location.href='/index.html';</script>");
//        }
//    }

    @RequestMapping(value = "/login.do", method = RequestMethod.POST)
    public void loginOut(String username, String password, HttpServletRequest hreq, HttpServletResponse hresp) throws IOException, InterruptedException {
        System.out.println(username);
        System.out.println(password);
//        UserHardCoded login = loginOutService.login(username, password);
        List<Hospusers> hospusers = loginOutService.selectHospUserByUsername(username, password);
        Hospusers login = hospusers.size() == 0? null:hospusers.get(0);
        if (login == null){
            hresp.setContentType("text/html;charset=utf-8");
            hresp.getWriter().write("<script>alert('登录失败，请重新登录！');location.href='/login.html';</script>");
        } else {
            hreq.getSession().invalidate();
            hreq.getSession().setAttribute("user", login);
            hresp.setContentType("text/html;charset=utf-8");
            hresp.getWriter().write("<script>location.href='/index.html';</script>");
        }
    }

    /**
     * 登录成功后页面显示用户名和用户id（在前端页面用户id的display被设置为none）
     * @param hreq
     * @return
     */
    @RequestMapping("/indexInit.do")
    @ResponseBody
    public ResultVO indexInit(HttpServletRequest hreq){
        HttpSession session = hreq.getSession();
        Hospusers user = (Hospusers) session.getAttribute("user");
        ResultVO<Hospusers> resultVO = new ResultVO<>();
        if (user != null){
            resultVO.setMessage("从session获取user成功");
            resultVO.setObj(user);
            return resultVO;
        }
        resultVO.setCode(500);
        resultVO.setMessage("获取用户名失败");
        return resultVO;
    }

    @RequestMapping("/logout.do")
    public void logout(HttpServletRequest hreq, HttpServletResponse hresp) throws IOException {
        hreq.getSession().invalidate();
        hresp.setContentType("text/html;charset=utf-8");
        hresp.getWriter().write("<script>location.href='/login.html'</script>");
    }

//    @RequestMapping("/logout.do")



    @RequestMapping("test")
    @ResponseBody
    public String test(){
        System.out.println("test");
        return loginOutService.selectUserHardCodedByName("joe", "").toString();
    }

    @RequestMapping(value = "/modifyPwd", method = RequestMethod.PUT)
    @ResponseBody
    public ResultVO<Object> modifyPwd(Hospusers hospusers){
        int i = loginOutService.modifyPwd(hospusers);
        ResultVO<Object> vo = new ResultVO<>();
        if (i != 1){
            vo.setCode(500);
            vo.setMessage("更新密码失败");
        }
        return vo;
    }

    @RequestMapping(value = "/user/list", method = RequestMethod.GET)
    @ResponseBody
    public ResultVO<UserWithRole> getUserInfoWithRole(@Nullable String username, Integer pageNumInput){
//        System.out.println(username);
//        System.out.println(pageNumInput);
        PageInfo<UserWithRole> userInfoWithRole = loginOutService.getUserInfoWithRole(username, pageNumInput, 5);
        ResultVO<UserWithRole> vo = new ResultVO<>();
        vo.setPageInfo(userInfoWithRole);
        return vo;
    }

    /**
     * 获取用户详细信息，用于更改用户信息时页面的初始化加载
     * @param userid
     * @return
     */
    @RequestMapping(value = "/userdetail/{userid}", method = RequestMethod.GET)
    @ResponseBody
    public ResultVO<Hospusers> getUserDetail(@PathVariable("userid") Integer userid){
        Hospusers hospUserDetail = loginOutService.getHospUserDetail(userid);
        ResultVO<Hospusers> vo = new ResultVO<>();
        vo.setObj(hospUserDetail);
        return vo;
    }

    /**
     * 获取角色列表
     * @return
     */
    @RequestMapping(value = "/rolelist", method = RequestMethod.GET)
    @ResponseBody
    public ResultVO<Role> getRoleList(){
        List<Role> roleList = loginOutService.getRoleList();
        ResultVO<Role> vo = new ResultVO<>();
        vo.setObjList(roleList);
        return vo;
    }

    @RequestMapping(value = "/updateUser", method = RequestMethod.PUT)
    @ResponseBody
    public ResultVO<Object> updateUser(Hospusers hospusers, Integer roleid){
        int i = loginOutService.updateUser(hospusers, roleid);
        ResultVO<Object> vo = new ResultVO<>();
        if (i != 1){
            vo.setCode(500);
            vo.setMessage("用户信息更新失败");
        }
        return vo;
    }

    @RequestMapping(value = "/shanchuyonghu", method = RequestMethod.PUT)
    @ResponseBody
    public ResultVO<Object> shanchuYonghu(@Param("userid") Integer userid){
        int shanchuYonghu = loginOutService.shanchuYonghu(userid);
        ResultVO<Object> resultVO = new ResultVO<>();
        if (shanchuYonghu != 1){
            resultVO.setCode(500);
            resultVO.setMessage("删除用户失败！");
        }
        resultVO.setMessage("停用用户成功！");
        return resultVO;
    }

    @RequestMapping(value = "/multiShanchuyonghu", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO<Object> multiShanchuyonghu(@RequestBody Map<String, ArrayList<Integer>> data){
        ArrayList<Integer> userids = data.get("userids");
        int i = loginOutService.multiDel(userids);
        ResultVO<Object> resultVO = new ResultVO<>();
        if (i <= 0){
            resultVO.setCode(500);
            resultVO.setMessage("批量删除用户失败");
        }
        resultVO.setMessage("批量删除用户成功");
        return resultVO;
    }

    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO<Object> addUser(Hospusers hospusers, Integer roleid){
        int adduser = loginOutService.addUser(hospusers, roleid);
        ResultVO<Object> vo = new ResultVO<>();
        if (adduser > 0){
            vo.setMessage("新增用户成功");
            return vo;
        }
        vo.setCode(500);
        vo.setMessage("新增用户失败");
        return vo;
    }
}
